/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Delegate;
import com.bourre.data.libs.GraphicLibEvent;

import fever.conf.ConfDebug;
import fever.conf.parsers.LibParser;
import fever.data.libs.SoundFactoryLocator;
import fever.data.libs.SoundLib;
import fever.Fever;
import fever.medias.sound.AppSoundFactory;

/**
 * Parses "sounds" definition from configuration file.
 * 
 * {@code
 *   <sounds>
 *     <factory id="Modern" url="modern.swf" ref="click1,click2" />
 *     <factory id="Classic" url="classic.swf" ref="click1,click2" />
 *  </sounds> 
 * }
 * 
 * @author Romain Ecarnot
 */
class fever.conf.parsers.SoundParser extends LibParser
{
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function SoundParser()
	{
		super();
	}
	
	/**
	 * Returns human readable text indicating parsing state.
	 */
	public function getParsingLabel() : String
	{
		return 'loading sounds / effects';	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _initStack() : Void
	{
		var factories : Object = Fever.application.config.sounds;
		
		for( var s in factories )
		{
			var factory : Object = factories[ s ];
			var factoryName : String = factory.id;	
			var factoryURL : String = factory.url;
			var factoryREF : Array = factory.ref.split( ',' );
			var isManaged : Boolean = ( factory.manager == 'true' );
			var isDefault : Boolean = ( factory.run == 'true' );
			
			var sf : AppSoundFactory;
			
			if( factoryName )
			{
				if( !SoundFactoryLocator.isRegistered( factoryName ) )
				{
					sf = SoundFactoryLocator.register( factoryName, new AppSoundFactory( factoryName ) );
				} 
				else sf = SoundFactoryLocator.getSoundFactory( factoryName );
			}
			else
			{
				factoryName = 'default';
				isManaged = false;
				isDefault = false;
				sf = SoundFactoryLocator.getSoundFactory();
			}
			
			if( factoryREF.length > 0 && factoryURL )
			{
				ConfDebug.DEBUG( 'create sound lib ' + factoryURL + ' -> ' + factoryREF + ' on ' + factoryName + '[ ' + isManaged + ' ]' );
			
				var slib : SoundLib = new SoundLib( factoryName ) ;
				slib.addEventListener( SoundLib.onLoadInitEVENT, Delegate.create( this, _onSoundLoaded, sf, factoryREF, isManaged, isDefault ) );
			
				_stack.enqueue( slib , factoryName, factoryURL );	
			}
			else
			{
				ConfDebug.WARN( 'no sounds references are defined for ' + factoryURL + ' library' );
			}
		}
	}
	
	private function _getLibPrefix() : String
	{	
		return Fever.application.config.getSoundPath();
	}
	
	private function _onSoundLoaded( event : GraphicLibEvent, sf : AppSoundFactory, soundsRef : Array, isManaged : Boolean, isRun : Boolean ) : Void
	{
		sf.init( event.getView() );
		sf.addSounds( soundsRef );
		
		if( isManaged ) AppSoundFactory.register( event.getLib().getName() );
		if( isRun ) AppSoundFactory.load( event.getLib().getName() );
	}
}